import { Component, OnInit } from '@angular/core';
import { UploadFile, MessageService } from '@iux/live';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-demo4',
  templateUrl: './demo4.component.html',
  styles: []
})
export class Demo4Component implements OnInit {

  constructor(private messageSerive: MessageService) { }

  ngOnInit() {
  }

  beforeUpload = (file: UploadFile, fileList: UploadFile[]) => {
    
    const supportSuffix = ['png', 'jpg'], fileSuffix = file.name.split('.').pop();
    
    // 不能上传png、jpg后缀以外文件
    if (!supportSuffix.includes(fileSuffix)) {
      this.messageSerive.error(`包含文件格式不正确，只支持.png,.jpg格式`);
      return false;
    }

    // 文件名必须包含字母a
    return new Observable((observer) => {
      const filename = file.name.split('.')[0], valid = filename.includes('a');

      if (!valid) {
        this.messageSerive.error(`文件名必须包含字母a`);
      }
      
      observer.next(valid);
      observer.complete();
      
    });

  }

  change(e) {
    console.log(e);
  }

}
